package com.qfedu.stussm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.stussm.dao.DeptDao;
import com.qfedu.stussm.dto.DeptDto;
import com.qfedu.stussm.dto.DeptEchartsDto;
import com.qfedu.stussm.entity.Dept;
import com.qfedu.stussm.service.intf.DeptService;
import com.qfedu.stussm.vo.DeptR;
import com.qfedu.stussm.vo.PageBeanVo;
import com.qfedu.stussm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao deptDao;

    @Override
    public DeptR insert(DeptDto dept) {
        if(deptDao.insert(dept)>0){
            return DeptR.ok();
        }else{
            return DeptR.fail();
        }

    }
    @Override
    public PageBeanVo select(String dept_name,int page, int limit) {
        PageHelper.startPage(page,limit);
        if(dept_name!=null && dept_name.length()>0){
            dept_name="%"+dept_name+"%";
            System.out.println(dept_name);
        }else{
            dept_name=null;
        }
        PageInfo<Dept> pageInfo = new PageInfo<>(deptDao.select(dept_name));
        return new PageBeanVo(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public DeptR del(Integer id) {
       if(deptDao.del(id)>0){
           return DeptR.ok();
       }else{
           return  DeptR.fail();
       }
    }

    @Override
    public DeptR update(DeptDto dept) {
        if(deptDao.update(dept)>0){
            return DeptR.ok();
        }else{
            return  DeptR.fail();
        }
    }
    @Override
    public DeptR dele(Integer[] ids) {
        if(deptDao.dele(ids)>0){
            return DeptR.ok();
        }else{
            return DeptR.fail();
        }
    }

    @Override
    public R deptEchartsSelect() {
        List<DeptEchartsDto> deptList = deptDao.selEcharts();
        ArrayList<Integer> nums = new ArrayList<>(deptList.size());
        ArrayList<String> names = new ArrayList<>(deptList.size());
        deptList.forEach(d->{
            nums.add(d.getNum());
            names.add(d.getDept_name());
        });
        HashMap<String, Object> map = new HashMap<>();
        map.put("num",nums);
        map.put("name",names);
        return R.ok(map);
    }

    @Override
    public void download(HttpServletResponse response) throws IOException {
        List<Dept> all = deptDao.all();
//        /*设置文件响应头*/
        response.setHeader("content-disposition","attachment;filename=dept_"+System.currentTimeMillis()+".xlsx");
        /*生成Excel文件并写出*/
         EasyExcel.write(response.getOutputStream(), Dept.class).sheet("部门表1").doWrite(all);
    }

    @Override
    public R upload(MultipartFile file) throws IOException {
        //1.验证上传内容 2.解析文件 3.批量新增
        //1.判断文件是否为空
        if(!file.isEmpty()){
            System.out.println("文件不是空！"+file.isEmpty());
            //2.获取上传内容
            InputStream is = file.getInputStream();
            //3.解析文件Excel
            EasyExcel.read(is,Dept.class,new PageReadListener<Dept>((list)->{
                //4.调用dao实现新增
                deptDao.addBatch(list);
            })).sheet().doRead();
        }
        System.out.println("文件执行完了！");
        return R.ok();
    }

}
